﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WinnerCMS.PlugIn.FileWatcher
{
    /// <summary>
    /// 只进行文件扫描
    /// </summary>
    public class ScanOnly
    {
        private int _count = 0;
        private bool ShowErrOnly;

        /// <summary>
        /// 扫描全站
        /// </summary>
        public void ScanAllStie(bool _ShowErrOnly)
        {
            ShowErrOnly = _ShowErrOnly;
            CommonHTMLCode();
            string strPath = System.Web.HttpContext.Current.Server.MapPath("~/");

            count(strPath);

            Scan_Total(_count);

            GetAllDirList(strPath);

            Scan_End();
        }

        /// <summary>
        /// 计算工作量
        /// </summary>
        /// <param name="strDirectory"></param>
        private void count(string strDirectory)
        {
            DirectoryInfo di = new DirectoryInfo(strDirectory);
            DirectoryInfo[] diA = di.GetDirectories();

            foreach (DirectoryInfo DI in diA)
            {
                _count++;
                count(DI.FullName);
            }
        }


        #region 取得下级目录所有文件
        /// <summary>
        /// 当前进度
        /// </summary>
        int _working = 0;

        /// <summary>
        /// 取得下级目录所有文件
        /// </summary>
        /// <param name="strFileName"></param>
        private void GetAllDirList(string strDirectory)
        {
            DirectoryInfo di = new DirectoryInfo(strDirectory);
            DirectoryInfo[] diA = di.GetDirectories();

            for (int i = 0; i < diA.Length; i++)
            {
                FileInfo[] Files = diA[i].GetFiles();
                foreach (FileInfo fi in Files)
                {
                    if (!fi.Exists)
                    {
                        continue;
                    }
                    //如果文件太大或者太小就跳过
                    if (Action.Ignore(fi.Length) || Action.Ignore(fi))
                    {
                        continue;
                    }


                    switch (fi.Extension.TrimStart('.').ToLower())
                    {
                        case "bmp":
                        case "com":
                        case "exe":
                        case "gif":
                        case "jpg":
                        case "jpeg":
                        case "png":
                        case "rar":
                        case "zip":
                            if (PE_Scaner.ValiFilePE(fi) != ResultOption.OK)
                            {
                                Scan_Percent(0, "非常可疑文件：" + fi.FullName.Replace(System.Web.HttpContext.Current.Server.MapPath("~/"), "\\").Replace("\\", "\\\\"));
                            }
                            else
                            {
                                if (Code_Scaner.CheckWebHorse(fi) == ResultOption.ItIsTrojan)
                                {
                                    Scan_Percent(0, "可疑文件：" + fi.FullName.Replace(System.Web.HttpContext.Current.Server.MapPath("~/"), "\\").Replace("\\", "\\\\"));
                                }
                            }
                            break;
                        default:
                            if (Code_Scaner.CheckWebHorse(fi) == ResultOption.ItIsTrojan)
                            {
                                Scan_Percent(0, "可疑文件：" + fi.FullName.Replace(System.Web.HttpContext.Current.Server.MapPath("~/"), "\\").Replace("\\", "\\\\"));
                            }
                            break;
                    }

                    System.Threading.Thread.Sleep(10);
                }
                _working++;

                if (ShowErrOnly == false)
                {
                    Scan_Percent(_working, "扫描完成：" + diA[i].FullName.Replace(System.Web.HttpContext.Current.Server.MapPath("~/"), "\\").Replace("\\", "\\\\"));
                }
                else
                {
                    Scan_Percent(_working, "");
                }

                GetAllDirList(diA[i].FullName);
            }
        }
        #endregion


        #region 共用的ＨＴＭＬ代码
        /// <summary>
        /// 共用的ＨＴＭＬ代码
        /// </summary>
        private void CommonHTMLCode()
        {

            System.Web.HttpContext.Current.Response.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            System.Web.HttpContext.Current.Response.Write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            System.Web.HttpContext.Current.Response.Write("<head>");
            System.Web.HttpContext.Current.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            System.Web.HttpContext.Current.Response.Write("<link href=\"../Style.css\" rel=\"stylesheet\" type=\"text/css\">");
System.Web.HttpContext.Current.Response.Write(" <link href=\"../../JS/JQ/UI/themes/redmond/jquery.ui.all.css\" rel=\"stylesheet\" type=\"text/css\" />");
            System.Web.HttpContext.Current.Response.Write("<script src=\"../../JS/JQ/jquery.js\" type=\"text/javascript\"></script>");
System.Web.HttpContext.Current.Response.Write(" <script src=\"../../JS/JQ/UI/ui/jquery-ui-1.8.22.custom.js\" type=\"text/javascript\"></script>");
            System.Web.HttpContext.Current.Response.Write("<script language=\"javascript\" type=\"text/javascript\" src=\"FileScan.js\"></script>");
            System.Web.HttpContext.Current.Response.Write("<title>全站扫描</title>");
            System.Web.HttpContext.Current.Response.Write("</head>");
            System.Web.HttpContext.Current.Response.Write("<body>");
            System.Web.HttpContext.Current.Response.Write("<div align=\"center\"><fieldset style=\"width:450px;\" ><legend>进度</legend> <div id=\"progressbar\"></div> <br><br> 详细 <div id=\"Note\" style=\"OVERFLOW: auto; WIDTH: 98%; HEIGHT: 400px;BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid;text-align: left; padding-left:10px\"></div></fieldset></div>");
            System.Web.HttpContext.Current.Response.Write("</body>");
            System.Web.HttpContext.Current.Response.Write("</html>");
        }
        /// <summary>
        /// 生成HTML的总量
        /// </summary>
        /// <param name="Total"></param>
        private void Scan_Total(int iTotal)
        {
            System.Web.HttpContext.Current.Response.Write(string.Format("<script> HTMLPageCount={0}; </script>", iTotal));
        }
        /// <summary>
        /// 进度百分比
        /// </summary>
        /// <param name="num"></param>
        /// <param name="strFilePath"></param>
        private void Scan_Percent(int inum, string strFilePath)
        {
            if (strFilePath.Length == 0)
            {
                System.Web.HttpContext.Current.Response.Write(string.Format("<script> PercentChange({0},\"\"); </script>", inum));
            }
            else
            {
                System.Web.HttpContext.Current.Response.Write(string.Format("<script> PercentChange({0},{1}); </script>", inum, "\"" + strFilePath + "<br>\""));
            }
        }
        /// <summary>
        /// 生成结束
        /// </summary>
        private void Scan_End()
        {
            System.Web.HttpContext.Current.Response.Write("<script> document.getElementById(\"Note\").innerHTML = '扫描完毕！<br>' + document.getElementById(\"Note\").innerHTML; </script>");
        }
        #endregion
    }
}